// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// y [author] rhyme_lph
// R [email]  rhymelph@gmail.com
// W [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/setting_app_entity.dart';
import 'package:finance_app/src/provider/setting/industry_select_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';

export 'package:finance_app/src/entities/setting_app_entity.dart';

@RouterMeta(paramName: "industrySelectPage")
class IndustrySelectPage extends StatefulWidget {
  final SettingAppIndustryType select;

  const IndustrySelectPage({Key key, this.select}) : super(key: key);

  @override
  _IndustrySelectPageState createState() => _IndustrySelectPageState();
}

class _IndustrySelectPageState extends State<IndustrySelectPage>
    with PagePresenter<IndustrySelectPage, IndustrySelectProvider> {
  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.industry,
      );

  @override
  void initState() {
    provider.currentSelect = widget.select;
    super.initState();
  }

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            AppSpaces.h20,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
            AppSpaces.h20,
            listBuilder<List<SettingAppIndustryType>>(
              selector: (_, __) => List.from(__.searchList),
              builder: (BuildContext context,
                      List<SettingAppIndustryType> value, Widget child) =>
                  Column(
                children: value
                    .map((e) => listBuilder<SettingAppIndustryType>(
                          selector: (_, __) => __.currentSelect,
                          builder: (BuildContext context,
                                  SettingAppIndustryType value, Widget child) =>
                              PressedOpacityButton(
                            padding: EdgeInsets.only(
                              left: getDp(28),
                              top: getDp(10),
                              bottom: getDp(10),
                            ),
                            onPressed: () => provider.onSelectType(e),
                            child: NormalRadio(
                              title: e.name,
                              isFix: true,
                              isSelect: value == e,
                            ),
                          ),
                        ))
                    .toList(),
              ),
            )
          ],
        ),
      );
}
